package com.jn.okhttppicasso_demo01.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jn.okhttppicasso_demo01.ImageDetailActivity;
import com.jn.okhttppicasso_demo01.R;
import com.jn.okhttppicasso_demo01.entity.ImageData;
import com.squareup.picasso.Picasso;

import java.util.List;

//public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
//    private Context context;
//    private static List<ImageData> dataList;
//
//    public ImageAdapter(Context context, List<ImageData> dataList) {
//        this.context = context;
//        this.dataList = dataList;
//    }
//
//    @NonNull
//    @Override
//    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(context).inflate(R.layout.item_image, parent, false);
//        return new ViewHolder(view);
//    }
//
//    @Override
//    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//        ImageData imageData = dataList.get(position);
//        holder.tvTitle.setText(imageData.getTitle());
//        Picasso.get().load(imageData.getUrl()).into(holder.ivImage);
//    }
//
//    @Override
//    public int getItemCount() {
//        return dataList.size();
//    }
//
//    public static class ViewHolder extends RecyclerView.ViewHolder {
//        public TextView tvTitle;
//        public ImageView ivImage;
//
//        public ViewHolder(@NonNull View itemView) {
//            super(itemView);
//            tvTitle = itemView.findViewById(R.id.tvTitle);
//            ivImage = itemView.findViewById(R.id.ivImage);
//            itemView.setOnClickListener(v -> {
//                // Handle click event, for example, open a new activity to show the image
//                int position = getAdapterPosition();
//                if (position != RecyclerView.NO_POSITION) {
//                    ImageData imageData = dataList.get(position);
//                    // You can start a new activity to show the image or handle it as needed
//                }
//            });
//        }
//    }
//}
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
    private static Context context;
    private static List<ImageData> dataList;

    public ImageAdapter(Context context, List<ImageData> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_image, parent, false);
        return new ViewHolder(view);
    }

//    @Override
//    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//        ImageData imageData = dataList.get(position);
//        holder.tvTitle.setText(imageData.getTitle());
//        Picasso.get().load(imageData.getUrl()).into(holder.ivImage);
//    }
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    ImageData imageData = dataList.get(position);
    holder.tvTitle.setText(imageData.getTitle());
    Picasso.get().load(imageData.getUrl()).into(holder.ivImage);

    // 设置标题点击事件
    holder.tvTitle.setOnClickListener(v -> {
        // 启动ImageDetailActivity并传递图片URL
        Intent intent = new Intent(context, ImageDetailActivity.class);
        intent.putExtra("IMAGE_URL", imageData.getUrl());
        context.startActivity(intent);
    });
}

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tvTitle;
        public ImageView ivImage;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            ivImage = itemView.findViewById(R.id.ivImage);
           // tvTitle.setOnClickListener(v -> {
              //  int position = getAdapterPosition();
//                if (position != RecyclerView.NO_POSITION) {
//                    ImageData imageData = dataList.get(position);
//                    // 启动ImageDetailActivity并传递图片URL
//                    context.startActivity(new Intent(context, ImageDetailActivity.class)
//                            .putExtra("IMAGE_URL", imageData.getUrl()));
//                }
 //           });
        }
    }
}